package controllers

import (
	"coinsend/models"
	"coinsend/utils"
	"strconv"

	"time"

	"github.com/astaxie/beego"
)

// Operations about Users
type WorkController struct {
	beego.Controller
}

//创建事务
// @router /addwork [post]
func (c *WorkController) Addwork() {

	var ret models.Response

	//获取Authorization
	Authorization := c.Ctx.Input.Header("Authorization")

	Billidstring := c.GetString("Billid")
	Symbol := c.GetString("Symbol")
	To := c.GetString("To")
	Amountstring := c.GetString("Amount")
	Timestampstring := c.GetString("Timestamp")
	str := "Amount" + Amountstring + "Billid" + Billidstring + "Symbol" + Symbol + "Timestamp" + Timestampstring + "To" + To
	Appkey := beego.AppConfig.String("Appkey")

	md5str := utils.GetMD5(Appkey + str + Appkey)

	if Authorization != md5str {
		ret.Code = 1
		ret.Msg = "添加发币事务失败,Authorization参数错误"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	Timestamp, _ := strconv.Atoi(Timestampstring)
	if Timestamp+10 <= int(time.Now().Unix()) {
		ret.Code = 1
		ret.Msg = "添加发币事务失败,时间戳超过10秒"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	Billid, err1 := strconv.Atoi(Billidstring)
	Amount, err2 := strconv.ParseFloat(Amountstring, 64)

	if Symbol == "" || To == "" || err1 != nil || err2 != nil {
		ret.Code = 1
		ret.Msg = "添加发币事务失败,参数错误"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	beego.Debug("创建事务", Symbol, To, Billid, Amount)
	//fmt.Println(string(c.Ctx.Input.RequestBody))
	err3 := models.ORMAddWork(Symbol, To, Billid, Amount)
	if err3 != nil {

		ret.Code = 1
		ret.Msg = "添加失败,原因:" + err3.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}

	beego.Debug("md5str"+md5str, "md5string"+Appkey+str+Appkey)
	ret.Code = 0
	ret.Msg = "创建事务成功"
	c.Data["json"] = ret
	c.ServeJSON()
}

//获取事务列表
// @router /listwork [post]
func (c *WorkController) Listwork() {

	var ret models.Response

	Status, err1 := strconv.Atoi(c.GetString("Status"))

	if err1 != nil {
		ret.Code = 1
		ret.Msg = "获取列表失败,参数错误"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	//fmt.Println(string(c.Ctx.Input.RequestBody))
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ := models.ParseUidFromToken(tokenString)
	data, err3 := models.GetListWork(Status, uid)
	if err3 != nil {

		ret.Code = 1
		ret.Msg = "获取列表失败,原因:" + err3.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	if len(data) <= 0 {
		ret.Data = []string{}
	} else {
		ret.Data = data
	}

	ret.Code = 0
	ret.Msg = ""
	c.Data["json"] = ret
	c.ServeJSON()
}

//获取待签名串
// @router /getunsigne [post]
func (c *WorkController) Getunsigne() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)
	Id, err1 := strconv.Atoi(c.GetString("Id"))

	if err1 != nil {
		ret.Code = 1
		ret.Msg = "获取待签名串失败,参数错误"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	//fmt.Println(string(c.Ctx.Input.RequestBody))
	beego.Debug("获取待签名串", Id, uid)
	unsigne, err3 := models.GetUnsigne(Id, uid)
	if err3 != nil {

		ret.Code = 1
		ret.Msg = "获取待签名串失败,原因:" + err3.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	beego.Debug("获取待签名串", Id, uid, unsigne, err3)
	ret.Code = 0
	ret.Msg = "获取待签名串成功"
	ret.Data = map[string]interface{}{
		"Unsigne": unsigne,
	}
	c.Data["json"] = ret
	c.ServeJSON()
}

//广播
// @router /sendsigned [post]
func (c *WorkController) Sendsigned() {

	var ret models.Response
	var uid int
	tokenString := c.Ctx.Input.Header("Authorization")
	uid, _ = models.ParseUidFromToken(tokenString)
	Id, err1 := strconv.Atoi(c.GetString("Id"))
	Signed := c.GetString("Signed")

	if err1 != nil || Signed == "" {
		ret.Code = 1
		ret.Msg = "广播失败,参数错误"
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	//fmt.Println(string(c.Ctx.Input.RequestBody))
	beego.Debug("广播", Id, uid, Signed)
	err3 := models.SendSigned(Id, uid, Signed)
	if err3 != nil {

		ret.Code = 1
		ret.Msg = "广播失败:" + err3.Error()
		c.Data["json"] = ret
		c.ServeJSON()
		return
	}
	beego.Debug("广播成功", Id, uid, Signed)
	ret.Code = 0
	ret.Msg = "广播成功"

	c.Data["json"] = ret
	c.ServeJSON()
}
